package com.wish.risk.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.wish.risk.domain.SaveFkBusinessCreditForm;
import com.wish.risk.dto.CreditApartmentResponseDto;
import com.wish.risk.dto.ViewDetailsResponseDto;
import com.wish.risk.service.ITFkBusinessCreditService;
import com.wish.risk.util.SendMsgUtil;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.PreAuthorize;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 风控公寓授信主Controller
 * 
 * @author ruoyi
 * @date 2021-06-19
 */
@RestController
@RequestMapping("/credit")
public class TFkBusinessCreditController extends BaseController
{
    @Autowired
    private ITFkBusinessCreditService tFkBusinessCreditService;

    @Autowired
    private RemoteFileService remoteFileService;
    /**
     * 查询风控公寓授信主列表
     */
    @PreAuthorize(hasPermi = "risk:credit:list")
    @GetMapping("/list")
    public TableDataInfo list(CreditApartmentResponseDto c)
    {
        startPage();
        List<CreditApartmentResponseDto> list = tFkBusinessCreditService.findCreditApartmentList(c);

        for (int i = 0; i < list.size(); i++) {

            BigDecimal hlTotalCreditline = list.get(i).getHl_total_creditline();
            BigDecimal hlUsedCreditline = list.get(i).getHl_used_creditline();

            if (hlTotalCreditline != null && hlUsedCreditline != null) {
                //算出总剩余可用额度
                BigDecimal subtract = hlTotalCreditline.subtract(hlUsedCreditline);
                //算出总已用额度%
                BigDecimal divide = (hlUsedCreditline.divide(hlTotalCreditline, 4, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(100));

                list.get(i).setHl_surplus_creditline(subtract);
                list.get(i).setHl_used_creditline_percent(divide);
            } else {
                if (list.get(i).getCredit_staus()!= null && list.get(i).getCredit_staus() == 1) {
                    list.get(i).setHl_surplus_creditline(hlTotalCreditline);
                    list.get(i).setHl_used_creditline_percent(BigDecimal.ZERO);
                }
            }

            BigDecimal hlMonthlyTotalCreditline = list.get(i).getHl_monthly_total_creditline();
            BigDecimal hlMonthlyUsedCreditline = list.get(i).getHl_monthly_used_creditline();

            if (hlMonthlyTotalCreditline != null && hlMonthlyUsedCreditline != null) {

                //算出月剩余可用额度
                BigDecimal subtractMonthly = hlMonthlyTotalCreditline.subtract(hlMonthlyUsedCreditline);
                //算出月已用额度%
                System.out.println("------------算出月已用额度%" + hlMonthlyUsedCreditline.divide(hlMonthlyTotalCreditline, 4, BigDecimal.ROUND_HALF_UP));
                BigDecimal divideMonthly = (hlMonthlyUsedCreditline.divide(hlMonthlyTotalCreditline, 4, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(100));

                list.get(i).setHl_monthly_surplus_creditline(subtractMonthly);
                list.get(i).setHl_monthly_used_creditline_percent(divideMonthly);
            } else {
                if (list.get(i).getCredit_staus() != null && list.get(i).getCredit_staus() == 1) {
                    list.get(i).setHl_monthly_surplus_creditline(hlMonthlyTotalCreditline);
                    list.get(i).setHl_monthly_used_creditline_percent(BigDecimal.ZERO);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 修改回显风控公寓授信主列表
     */
   @PreAuthorize(hasPermi = "risk:credit:edit")
    @GetMapping("/edit")
    public ViewDetailsResponseDto edit(ViewDetailsResponseDto viewDetailsResponseDto){

        return tFkBusinessCreditService.findById(viewDetailsResponseDto);
    }

    /**
     *
     *  修改或添加公寓授信数据
     */
    @RequestMapping("update")
    @ResponseBody
    public boolean updateFkBusinessCredit(@Validated SaveFkBusinessCreditForm saveFkBusinessCreditForm) {

        return  tFkBusinessCreditService.update(saveFkBusinessCreditForm);

    }

    /*
        发送短信
     */
    @RequestMapping("send")
    public String send(){
        SendMsgUtil.send("19525506510",123);
        return "发送成功";
    }


    @RequestMapping("upload")
    public Object upload(MultipartFile file){
        R<SysFile> upload = remoteFileService.upload(file);

        return upload.getData().getUrl();
    }

}
